package pe.gob.mpfn.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.validator.LengthValidator;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.message.Message;
import org.primefaces.component.spinner.Spinner;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.gob.mpfn.controller.converter.TablaMaestraConverter;
import pe.gob.mpfn.model.ExpedienteParte;
import pe.gob.mpfn.model.Parte;
import pe.gob.mpfn.model.Proteccion;
import pe.gob.mpfn.model.TablaMaestra;
import pe.gob.mpfn.service.ParteService;
import pe.gob.mpfn.service.TablaMaestraService;

@Configurable
@ManagedBean(name = "parteBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = Parte.class, beanName = "parteBean")
public class ParteBean implements Serializable  {

	private static final long serialVersionUID = 1L;

	@Autowired
    ParteService parteService;

	@Autowired
    TablaMaestraService tablaMaestraService;

	private String name = "Partes";

	private Parte parte;

	private List<Parte> allPartes;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<ExpedienteParte> selectedExpedientePartes;

	private List<Proteccion> selectedProteccions;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("strNombre");
        columns.add("strApellidoParterno");
        columns.add("strApellidoMaterno");
        columns.add("intEdad");
        columns.add("strNumeroDocumento");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Parte> getAllPartes() {
        return allPartes;
    }

	public void setAllPartes(List<Parte> allPartes) {
        this.allPartes = allPartes;
    }

	public String findAllPartes() {
        allPartes = parteService.findAllPartes();
        dataVisible = !allPartes.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        

        
      
        
        HtmlOutputText intTipoDocumentoIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoDocumentoIdCreateOutput.setId("intTipoDocumentoIdCreateOutput");
        intTipoDocumentoIdCreateOutput.setValue("Int Tipo Documento Id:   ");
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdCreateOutput);
        
        AutoComplete intTipoDocumentoIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intTipoDocumentoIdCreateInput.setId("intTipoDocumentoIdCreateInput");
        intTipoDocumentoIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intTipoDocumentoId}", TablaMaestra.class));
        intTipoDocumentoIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{parteBean.completeIntTipoDocumentoId}", List.class, new Class[] { String.class }));
        intTipoDocumentoIdCreateInput.setDropdown(true);
        intTipoDocumentoIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intTipoDocumentoId", String.class));
        intTipoDocumentoIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intTipoDocumentoId.strDescripcion} #{intTipoDocumentoId.chrEstado} #{intTipoDocumentoId.intTablaId}", String.class));
        intTipoDocumentoIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intTipoDocumentoId}", TablaMaestra.class));
        intTipoDocumentoIdCreateInput.setConverter(new TablaMaestraConverter());
        intTipoDocumentoIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdCreateInput);
        
        Message intTipoDocumentoIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intTipoDocumentoIdCreateInputMessage.setId("intTipoDocumentoIdCreateInputMessage");
        intTipoDocumentoIdCreateInputMessage.setFor("intTipoDocumentoIdCreateInput");
        intTipoDocumentoIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdCreateInputMessage);
        
        HtmlOutputText intTipoParteIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoParteIdCreateOutput.setId("intTipoParteIdCreateOutput");
        intTipoParteIdCreateOutput.setValue("Int Tipo Parte Id:   ");
        htmlPanelGrid.getChildren().add(intTipoParteIdCreateOutput);
        
        AutoComplete intTipoParteIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intTipoParteIdCreateInput.setId("intTipoParteIdCreateInput");
        intTipoParteIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intTipoParteId}", TablaMaestra.class));
        intTipoParteIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{parteBean.completeIntTipoParteId}", List.class, new Class[] { String.class }));
        intTipoParteIdCreateInput.setDropdown(true);
        intTipoParteIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intTipoParteId", String.class));
        intTipoParteIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intTipoParteId.strDescripcion} #{intTipoParteId.chrEstado} #{intTipoParteId.intTablaId}", String.class));
        intTipoParteIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intTipoParteId}", TablaMaestra.class));
        intTipoParteIdCreateInput.setConverter(new TablaMaestraConverter());
        intTipoParteIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intTipoParteIdCreateInput);
        
        Message intTipoParteIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intTipoParteIdCreateInputMessage.setId("intTipoParteIdCreateInputMessage");
        intTipoParteIdCreateInputMessage.setFor("intTipoParteIdCreateInput");
        intTipoParteIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intTipoParteIdCreateInputMessage);
        
        HtmlOutputText strNombreCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNombreCreateOutput.setId("strNombreCreateOutput");
        strNombreCreateOutput.setValue("Str Nombre: * ");
        htmlPanelGrid.getChildren().add(strNombreCreateOutput);
        
        InputTextarea strNombreCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNombreCreateInput.setId("strNombreCreateInput");
        strNombreCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strNombre}", String.class));
        LengthValidator strNombreCreateInputValidator = new LengthValidator();
        strNombreCreateInputValidator.setMaximum(45);
        strNombreCreateInput.addValidator(strNombreCreateInputValidator);
        strNombreCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strNombreCreateInput);
        
        Message strNombreCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNombreCreateInputMessage.setId("strNombreCreateInputMessage");
        strNombreCreateInputMessage.setFor("strNombreCreateInput");
        strNombreCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNombreCreateInputMessage);
        
        HtmlOutputText strApellidoParternoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoParternoCreateOutput.setId("strApellidoParternoCreateOutput");
        strApellidoParternoCreateOutput.setValue("Str Apellido Parterno: * ");
        htmlPanelGrid.getChildren().add(strApellidoParternoCreateOutput);
        
        InputTextarea strApellidoParternoCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoParternoCreateInput.setId("strApellidoParternoCreateInput");
        strApellidoParternoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strApellidoParterno}", String.class));
        LengthValidator strApellidoParternoCreateInputValidator = new LengthValidator();
        strApellidoParternoCreateInputValidator.setMaximum(45);
        strApellidoParternoCreateInput.addValidator(strApellidoParternoCreateInputValidator);
        strApellidoParternoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoParternoCreateInput);
        
        Message strApellidoParternoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoParternoCreateInputMessage.setId("strApellidoParternoCreateInputMessage");
        strApellidoParternoCreateInputMessage.setFor("strApellidoParternoCreateInput");
        strApellidoParternoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoParternoCreateInputMessage);
        
        HtmlOutputText strApellidoMaternoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoMaternoCreateOutput.setId("strApellidoMaternoCreateOutput");
        strApellidoMaternoCreateOutput.setValue("Str Apellido Materno: * ");
        htmlPanelGrid.getChildren().add(strApellidoMaternoCreateOutput);
        
        InputTextarea strApellidoMaternoCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoMaternoCreateInput.setId("strApellidoMaternoCreateInput");
        strApellidoMaternoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strApellidoMaterno}", String.class));
        LengthValidator strApellidoMaternoCreateInputValidator = new LengthValidator();
        strApellidoMaternoCreateInputValidator.setMaximum(45);
        strApellidoMaternoCreateInput.addValidator(strApellidoMaternoCreateInputValidator);
        strApellidoMaternoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoMaternoCreateInput);
        
        Message strApellidoMaternoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoMaternoCreateInputMessage.setId("strApellidoMaternoCreateInputMessage");
        strApellidoMaternoCreateInputMessage.setFor("strApellidoMaternoCreateInput");
        strApellidoMaternoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoMaternoCreateInputMessage);
        
        HtmlOutputText intEdadCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intEdadCreateOutput.setId("intEdadCreateOutput");
        intEdadCreateOutput.setValue("Int Edad: * ");
        htmlPanelGrid.getChildren().add(intEdadCreateOutput);
        
        Spinner intEdadCreateInput = (Spinner) application.createComponent(Spinner.COMPONENT_TYPE);
        intEdadCreateInput.setId("intEdadCreateInput");
        intEdadCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intEdad}", Integer.class));
        intEdadCreateInput.setRequired(true);
        
        htmlPanelGrid.getChildren().add(intEdadCreateInput);
        
        Message intEdadCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intEdadCreateInputMessage.setId("intEdadCreateInputMessage");
        intEdadCreateInputMessage.setFor("intEdadCreateInput");
        intEdadCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intEdadCreateInputMessage);
        
        HtmlOutputText strNumeroDocumentoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroDocumentoCreateOutput.setId("strNumeroDocumentoCreateOutput");
        strNumeroDocumentoCreateOutput.setValue("Str Numero Documento: * ");
        htmlPanelGrid.getChildren().add(strNumeroDocumentoCreateOutput);
        
        InputTextarea strNumeroDocumentoCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNumeroDocumentoCreateInput.setId("strNumeroDocumentoCreateInput");
        strNumeroDocumentoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strNumeroDocumento}", String.class));
        LengthValidator strNumeroDocumentoCreateInputValidator = new LengthValidator();
        strNumeroDocumentoCreateInputValidator.setMaximum(50);
        strNumeroDocumentoCreateInput.addValidator(strNumeroDocumentoCreateInputValidator);
        strNumeroDocumentoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strNumeroDocumentoCreateInput);
        
        Message strNumeroDocumentoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNumeroDocumentoCreateInputMessage.setId("strNumeroDocumentoCreateInputMessage");
        strNumeroDocumentoCreateInputMessage.setFor("strNumeroDocumentoCreateInput");
        strNumeroDocumentoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNumeroDocumentoCreateInputMessage);
        
        HtmlOutputText strEstadoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strEstadoCreateOutput.setId("strEstadoCreateOutput");
        strEstadoCreateOutput.setValue("Str Estado:   ");
        htmlPanelGrid.getChildren().add(strEstadoCreateOutput);
        
        InputText strEstadoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        strEstadoCreateInput.setId("strEstadoCreateInput");
        strEstadoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strEstado}", Character.class));
        strEstadoCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(strEstadoCreateInput);
        
        Message strEstadoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strEstadoCreateInputMessage.setId("strEstadoCreateInputMessage");
        strEstadoCreateInputMessage.setFor("strEstadoCreateInput");
        strEstadoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strEstadoCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText expedientePartesEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        expedientePartesEditOutput.setId("expedientePartesEditOutput");
        expedientePartesEditOutput.setValue("Expediente Partes:   ");
        htmlPanelGrid.getChildren().add(expedientePartesEditOutput);
        
        HtmlOutputText expedientePartesEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        expedientePartesEditInput.setId("expedientePartesEditInput");
        expedientePartesEditInput.setValue("This relationship is managed from the ExpedienteParte side");
        htmlPanelGrid.getChildren().add(expedientePartesEditInput);
        
        Message expedientePartesEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        expedientePartesEditInputMessage.setId("expedientePartesEditInputMessage");
        expedientePartesEditInputMessage.setFor("expedientePartesEditInput");
        expedientePartesEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(expedientePartesEditInputMessage);
        
        HtmlOutputText proteccionsEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        proteccionsEditOutput.setId("proteccionsEditOutput");
        proteccionsEditOutput.setValue("Proteccions:   ");
        htmlPanelGrid.getChildren().add(proteccionsEditOutput);
        
        HtmlOutputText proteccionsEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        proteccionsEditInput.setId("proteccionsEditInput");
        proteccionsEditInput.setValue("This relationship is managed from the Proteccion side");
        htmlPanelGrid.getChildren().add(proteccionsEditInput);
        
        Message proteccionsEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        proteccionsEditInputMessage.setId("proteccionsEditInputMessage");
        proteccionsEditInputMessage.setFor("proteccionsEditInput");
        proteccionsEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(proteccionsEditInputMessage);
        
        HtmlOutputText intTipoDocumentoIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoDocumentoIdEditOutput.setId("intTipoDocumentoIdEditOutput");
        intTipoDocumentoIdEditOutput.setValue("Int Tipo Documento Id:   ");
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdEditOutput);
        
        AutoComplete intTipoDocumentoIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intTipoDocumentoIdEditInput.setId("intTipoDocumentoIdEditInput");
        intTipoDocumentoIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intTipoDocumentoId}", TablaMaestra.class));
        intTipoDocumentoIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{parteBean.completeIntTipoDocumentoId}", List.class, new Class[] { String.class }));
        intTipoDocumentoIdEditInput.setDropdown(true);
        intTipoDocumentoIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intTipoDocumentoId", String.class));
        intTipoDocumentoIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intTipoDocumentoId.strDescripcion} #{intTipoDocumentoId.chrEstado} #{intTipoDocumentoId.intTablaId}", String.class));
        intTipoDocumentoIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intTipoDocumentoId}", TablaMaestra.class));
        intTipoDocumentoIdEditInput.setConverter(new TablaMaestraConverter());
        intTipoDocumentoIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdEditInput);
        
        Message intTipoDocumentoIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intTipoDocumentoIdEditInputMessage.setId("intTipoDocumentoIdEditInputMessage");
        intTipoDocumentoIdEditInputMessage.setFor("intTipoDocumentoIdEditInput");
        intTipoDocumentoIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdEditInputMessage);
        
        HtmlOutputText intTipoParteIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoParteIdEditOutput.setId("intTipoParteIdEditOutput");
        intTipoParteIdEditOutput.setValue("Int Tipo Parte Id:   ");
        htmlPanelGrid.getChildren().add(intTipoParteIdEditOutput);
        
        AutoComplete intTipoParteIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intTipoParteIdEditInput.setId("intTipoParteIdEditInput");
        intTipoParteIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intTipoParteId}", TablaMaestra.class));
        intTipoParteIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{parteBean.completeIntTipoParteId}", List.class, new Class[] { String.class }));
        intTipoParteIdEditInput.setDropdown(true);
        intTipoParteIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intTipoParteId", String.class));
        intTipoParteIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intTipoParteId.strDescripcion} #{intTipoParteId.chrEstado} #{intTipoParteId.intTablaId}", String.class));
        intTipoParteIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intTipoParteId}", TablaMaestra.class));
        intTipoParteIdEditInput.setConverter(new TablaMaestraConverter());
        intTipoParteIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intTipoParteIdEditInput);
        
        Message intTipoParteIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intTipoParteIdEditInputMessage.setId("intTipoParteIdEditInputMessage");
        intTipoParteIdEditInputMessage.setFor("intTipoParteIdEditInput");
        intTipoParteIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intTipoParteIdEditInputMessage);
        
        HtmlOutputText strNombreEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNombreEditOutput.setId("strNombreEditOutput");
        strNombreEditOutput.setValue("Str Nombre: * ");
        htmlPanelGrid.getChildren().add(strNombreEditOutput);
        
        InputTextarea strNombreEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNombreEditInput.setId("strNombreEditInput");
        strNombreEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strNombre}", String.class));
        LengthValidator strNombreEditInputValidator = new LengthValidator();
        strNombreEditInputValidator.setMaximum(45);
        strNombreEditInput.addValidator(strNombreEditInputValidator);
        strNombreEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strNombreEditInput);
        
        Message strNombreEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNombreEditInputMessage.setId("strNombreEditInputMessage");
        strNombreEditInputMessage.setFor("strNombreEditInput");
        strNombreEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNombreEditInputMessage);
        
        HtmlOutputText strApellidoParternoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoParternoEditOutput.setId("strApellidoParternoEditOutput");
        strApellidoParternoEditOutput.setValue("Str Apellido Parterno: * ");
        htmlPanelGrid.getChildren().add(strApellidoParternoEditOutput);
        
        InputTextarea strApellidoParternoEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoParternoEditInput.setId("strApellidoParternoEditInput");
        strApellidoParternoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strApellidoParterno}", String.class));
        LengthValidator strApellidoParternoEditInputValidator = new LengthValidator();
        strApellidoParternoEditInputValidator.setMaximum(45);
        strApellidoParternoEditInput.addValidator(strApellidoParternoEditInputValidator);
        strApellidoParternoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoParternoEditInput);
        
        Message strApellidoParternoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoParternoEditInputMessage.setId("strApellidoParternoEditInputMessage");
        strApellidoParternoEditInputMessage.setFor("strApellidoParternoEditInput");
        strApellidoParternoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoParternoEditInputMessage);
        
        HtmlOutputText strApellidoMaternoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoMaternoEditOutput.setId("strApellidoMaternoEditOutput");
        strApellidoMaternoEditOutput.setValue("Str Apellido Materno: * ");
        htmlPanelGrid.getChildren().add(strApellidoMaternoEditOutput);
        
        InputTextarea strApellidoMaternoEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoMaternoEditInput.setId("strApellidoMaternoEditInput");
        strApellidoMaternoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strApellidoMaterno}", String.class));
        LengthValidator strApellidoMaternoEditInputValidator = new LengthValidator();
        strApellidoMaternoEditInputValidator.setMaximum(45);
        strApellidoMaternoEditInput.addValidator(strApellidoMaternoEditInputValidator);
        strApellidoMaternoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoMaternoEditInput);
        
        Message strApellidoMaternoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoMaternoEditInputMessage.setId("strApellidoMaternoEditInputMessage");
        strApellidoMaternoEditInputMessage.setFor("strApellidoMaternoEditInput");
        strApellidoMaternoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoMaternoEditInputMessage);
        
        HtmlOutputText intEdadEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intEdadEditOutput.setId("intEdadEditOutput");
        intEdadEditOutput.setValue("Int Edad: * ");
        htmlPanelGrid.getChildren().add(intEdadEditOutput);
        
        Spinner intEdadEditInput = (Spinner) application.createComponent(Spinner.COMPONENT_TYPE);
        intEdadEditInput.setId("intEdadEditInput");
        intEdadEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intEdad}", Integer.class));
        intEdadEditInput.setRequired(true);
        
        htmlPanelGrid.getChildren().add(intEdadEditInput);
        
        Message intEdadEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intEdadEditInputMessage.setId("intEdadEditInputMessage");
        intEdadEditInputMessage.setFor("intEdadEditInput");
        intEdadEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intEdadEditInputMessage);
        
        HtmlOutputText strNumeroDocumentoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroDocumentoEditOutput.setId("strNumeroDocumentoEditOutput");
        strNumeroDocumentoEditOutput.setValue("Str Numero Documento: * ");
        htmlPanelGrid.getChildren().add(strNumeroDocumentoEditOutput);
        
        InputTextarea strNumeroDocumentoEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNumeroDocumentoEditInput.setId("strNumeroDocumentoEditInput");
        strNumeroDocumentoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strNumeroDocumento}", String.class));
        LengthValidator strNumeroDocumentoEditInputValidator = new LengthValidator();
        strNumeroDocumentoEditInputValidator.setMaximum(50);
        strNumeroDocumentoEditInput.addValidator(strNumeroDocumentoEditInputValidator);
        strNumeroDocumentoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strNumeroDocumentoEditInput);
        
        Message strNumeroDocumentoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNumeroDocumentoEditInputMessage.setId("strNumeroDocumentoEditInputMessage");
        strNumeroDocumentoEditInputMessage.setFor("strNumeroDocumentoEditInput");
        strNumeroDocumentoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNumeroDocumentoEditInputMessage);
        
        HtmlOutputText strEstadoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strEstadoEditOutput.setId("strEstadoEditOutput");
        strEstadoEditOutput.setValue("Str Estado:   ");
        htmlPanelGrid.getChildren().add(strEstadoEditOutput);
        
        InputText strEstadoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        strEstadoEditInput.setId("strEstadoEditInput");
        strEstadoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strEstado}", Character.class));
        strEstadoEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(strEstadoEditInput);
        
        Message strEstadoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strEstadoEditInputMessage.setId("strEstadoEditInputMessage");
        strEstadoEditInputMessage.setFor("strEstadoEditInput");
        strEstadoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strEstadoEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText expedientePartesLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        expedientePartesLabel.setId("expedientePartesLabel");
        expedientePartesLabel.setValue("Expediente Partes:   ");
        htmlPanelGrid.getChildren().add(expedientePartesLabel);
        
        HtmlOutputText expedientePartesValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        expedientePartesValue.setId("expedientePartesValue");
        expedientePartesValue.setValue("This relationship is managed from the ExpedienteParte side");
        htmlPanelGrid.getChildren().add(expedientePartesValue);
        
        HtmlOutputText proteccionsLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        proteccionsLabel.setId("proteccionsLabel");
        proteccionsLabel.setValue("Proteccions:   ");
        htmlPanelGrid.getChildren().add(proteccionsLabel);
        
        HtmlOutputText proteccionsValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        proteccionsValue.setId("proteccionsValue");
        proteccionsValue.setValue("This relationship is managed from the Proteccion side");
        htmlPanelGrid.getChildren().add(proteccionsValue);
        
        HtmlOutputText intTipoDocumentoIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoDocumentoIdLabel.setId("intTipoDocumentoIdLabel");
        intTipoDocumentoIdLabel.setValue("Int Tipo Documento Id:   ");
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdLabel);
        
        HtmlOutputText intTipoDocumentoIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoDocumentoIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intTipoDocumentoId}", TablaMaestra.class));
        intTipoDocumentoIdValue.setConverter(new TablaMaestraConverter());
        htmlPanelGrid.getChildren().add(intTipoDocumentoIdValue);
        
        HtmlOutputText intTipoParteIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoParteIdLabel.setId("intTipoParteIdLabel");
        intTipoParteIdLabel.setValue("Int Tipo Parte Id:   ");
        htmlPanelGrid.getChildren().add(intTipoParteIdLabel);
        
        HtmlOutputText intTipoParteIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intTipoParteIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intTipoParteId}", TablaMaestra.class));
        intTipoParteIdValue.setConverter(new TablaMaestraConverter());
        htmlPanelGrid.getChildren().add(intTipoParteIdValue);
        
        HtmlOutputText strNombreLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNombreLabel.setId("strNombreLabel");
        strNombreLabel.setValue("Str Nombre:   ");
        htmlPanelGrid.getChildren().add(strNombreLabel);
        
        InputTextarea strNombreValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNombreValue.setId("strNombreValue");
        strNombreValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strNombre}", String.class));
        strNombreValue.setReadonly(true);
        strNombreValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strNombreValue);
        
        HtmlOutputText strApellidoParternoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoParternoLabel.setId("strApellidoParternoLabel");
        strApellidoParternoLabel.setValue("Str Apellido Parterno:   ");
        htmlPanelGrid.getChildren().add(strApellidoParternoLabel);
        
        InputTextarea strApellidoParternoValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoParternoValue.setId("strApellidoParternoValue");
        strApellidoParternoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strApellidoParterno}", String.class));
        strApellidoParternoValue.setReadonly(true);
        strApellidoParternoValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strApellidoParternoValue);
        
        HtmlOutputText strApellidoMaternoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoMaternoLabel.setId("strApellidoMaternoLabel");
        strApellidoMaternoLabel.setValue("Str Apellido Materno:   ");
        htmlPanelGrid.getChildren().add(strApellidoMaternoLabel);
        
        InputTextarea strApellidoMaternoValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoMaternoValue.setId("strApellidoMaternoValue");
        strApellidoMaternoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strApellidoMaterno}", String.class));
        strApellidoMaternoValue.setReadonly(true);
        strApellidoMaternoValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strApellidoMaternoValue);
        
        HtmlOutputText intEdadLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intEdadLabel.setId("intEdadLabel");
        intEdadLabel.setValue("Int Edad:   ");
        htmlPanelGrid.getChildren().add(intEdadLabel);
        
        HtmlOutputText intEdadValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intEdadValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.intEdad}", String.class));
        htmlPanelGrid.getChildren().add(intEdadValue);
        
        HtmlOutputText strNumeroDocumentoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroDocumentoLabel.setId("strNumeroDocumentoLabel");
        strNumeroDocumentoLabel.setValue("Str Numero Documento:   ");
        htmlPanelGrid.getChildren().add(strNumeroDocumentoLabel);
        
        InputTextarea strNumeroDocumentoValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNumeroDocumentoValue.setId("strNumeroDocumentoValue");
        strNumeroDocumentoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strNumeroDocumento}", String.class));
        strNumeroDocumentoValue.setReadonly(true);
        strNumeroDocumentoValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strNumeroDocumentoValue);
        
        HtmlOutputText strEstadoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strEstadoLabel.setId("strEstadoLabel");
        strEstadoLabel.setValue("Str Estado:   ");
        htmlPanelGrid.getChildren().add(strEstadoLabel);
        
        HtmlOutputText strEstadoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strEstadoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{parteBean.parte.strEstado}", String.class));
        htmlPanelGrid.getChildren().add(strEstadoValue);
        
        return htmlPanelGrid;
    }

	public Parte getParte() {
        if (parte == null) {
            parte = new Parte();
        }
        return parte;
    }

	public void setParte(Parte parte) {
        this.parte = parte;
    }

	public List<ExpedienteParte> getSelectedExpedientePartes() {
        return selectedExpedientePartes;
    }

	public void setSelectedExpedientePartes(List<ExpedienteParte> selectedExpedientePartes) {
        if (selectedExpedientePartes != null) {
            parte.setExpedientePartes(new HashSet<ExpedienteParte>(selectedExpedientePartes));
        }
        this.selectedExpedientePartes = selectedExpedientePartes;
    }

	public List<Proteccion> getSelectedProteccions() {
        return selectedProteccions;
    }

	public void setSelectedProteccions(List<Proteccion> selectedProteccions) {
        if (selectedProteccions != null) {
            parte.setProteccions(new HashSet<Proteccion>(selectedProteccions));
        }
        this.selectedProteccions = selectedProteccions;
    }

	public List<TablaMaestra> completeIntTipoDocumentoId(String query) {
        List<TablaMaestra> suggestions = new ArrayList<TablaMaestra>();
        for (TablaMaestra tablaMaestra : tablaMaestraService.findAllTablaMaestras()) {
            String tablaMaestraStr = String.valueOf(tablaMaestra.getStrDescripcion() +  " "  + tablaMaestra.getChrEstado() +  " "  + tablaMaestra.getIntTablaId());
            if (tablaMaestraStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(tablaMaestra);
            }
        }
        return suggestions;
    }

	public List<TablaMaestra> completeIntTipoParteId(String query) {
        List<TablaMaestra> suggestions = new ArrayList<TablaMaestra>();
        for (TablaMaestra tablaMaestra : tablaMaestraService.findAllTablaMaestras()) {
            String tablaMaestraStr = String.valueOf(tablaMaestra.getStrDescripcion() +  " "  + tablaMaestra.getChrEstado() +  " "  + tablaMaestra.getIntTablaId());
            if (tablaMaestraStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(tablaMaestra);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        if (parte != null && parte.getExpedientePartes() != null) {
            selectedExpedientePartes = new ArrayList<ExpedienteParte>(parte.getExpedientePartes());
        }
        if (parte != null && parte.getProteccions() != null) {
            selectedProteccions = new ArrayList<Proteccion>(parte.getProteccions());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllPartes();
        return "parte";
    }

	public String displayCreateDialog() {
        parte = new Parte();
        createDialogVisible = true;
        return "parte";
    }

	public String persist() {
        String message = "";
        if (parte.getIntParteId() != null) {
            parteService.updateParte(parte);
            message = "Successfully updated";
        } else {
            parteService.saveParte(parte);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllPartes();
    }

	public String delete() {
        parteService.deleteParte(parte);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllPartes();
    }

	public void reset() {
        parte = null;
        selectedExpedientePartes = null;
        selectedProteccions = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }
}
